(function(){
	Ext.define('AppView.base.BaseTreeGrid',{
		extend : 'Ext.tree.Panel',
		alias:'widget.basetreegrid',
		selType: 'checkboxmodel',
		rootVisible : false,
		useArrows : true, // 箭头节点图标
		editWin:'',
		gridStore:'',
		moduleUrl:'',//controller
		gridTbar:[],//grid自定义按钮
		btConfig:{//默认按钮
			add:true,
			edit:true,
			del:true,
			refresh:true
		},
		tbar:[
	    	{
	    		text:'添加',
	    		name:'add',
	    		icon:'resources/button/add.png',
	    		handler:function(){
	    			var treeGrid = this.up('basetreegrid');
	    			if(treeGrid.editWin){
	    				var formWin = Ext.create(this.up('basetreegrid').editWin);
	    				formWin.parentComp = treeGrid;
	    				formWin.show();
	    			}
	    		}
	    	},
	    	{
	    		text:'编辑',
	    		name:'edit',
	    		icon:'resources/button/edit.png',
	    		handler:function(){
	    			var treeGrid = this.up('basetreegrid');
					var selection = treeGrid.getSelectionModel().getSelection();
					if(selection.length==0 || selection.length>1){
						Ext.Msg.alert('提示','请选择一条记录进行编辑!');
						return;
					}
	    			var selRecord = selection[0];
	    			if(treeGrid.editWin){
	    				var formWin = Ext.create(this.up('basetreegrid').editWin);
	    				formWin.parentComp = treeGrid;
	    				if(!formWin.viewUrl){
	    					Ext.Msg.alert('提示','请设置viewUrl!');
	    					return;
	    				}
	    				formWin.show();
	    				var targetForm = formWin.down('form');
	    				var childForm = targetForm.getForm();
	    				var loadParams={
	    					url:formWin.viewUrl,
	    					method:'post',
	    					params:{id:selRecord.raw.id},
	    					success:function(form,action){
	    						targetForm.handleLoadSuccess(action.result);
	    					},

							failure:function(form,action){
								Ext.Msg.alert('提示',"失败原因是: "+action.result.message);
							}
	    				}
	    				childForm.load(loadParams);
	    			}
	    		}
	    	},
    		{
	    		text:'删除',
	    		name:'del',
	    		icon:'resources/button/del.png',
	    		handler:function(){
	    			var treeGrid = this.up('basetreegrid');
					var selection = treeGrid.getSelectionModel().getSelection();
					if(selection.length==0){
						Ext.Msg.alert('提示','请选择记录进行删除!');
						return;
					}
					var ids = [];
					Ext.each(selection,function(data){
						ids.push(data.raw.id)
					});
					if(!treeGrid.moduleUrl){
						Ext.Msg.alert('提示','请设置moduleUrl!');
					};
					Ext.Msg.confirm('提示','确定要删除选中的记录吗?',function(optional){
						if(optional=='yes'){
							Ext.Ajax.request({
								url : treeGrid.moduleUrl+'/del.do',
								params : {
									ids : ids
								},
								success : function(response) {
									var text = Ext.decode(response.responseText);
									Ext.Msg.alert('提示',text.message);
									treeGrid.getStore().reload();
								},
								failure : function(response) {
									var text = Ext.decode(response.responseText);
									Ext.Msg.alert('提示',text.data.message);
								}
							});
						}else{
						
						}
					})
	    		}
    		},
    		{
	    		text:'刷新',
	    		name:'refresh',
	    		icon:'resources/button/refresh.png',
	    		handler:function(){
	    			var treeGrid = this.up('basetreegrid');
	    			treeGrid.getStore().reload();
	    		}
    		}
	    ],
	    initComponent:function(){
	    	var treeGridStore = Ext.create(this.treeGridStore);
	    	this.store = treeGridStore;
	    	
//	    	//设置底部分页
//	    	var pagingtoolbar = {
//		        xtype: 'pagingtoolbar',
//		        store: gridStore,   // same store GridPanel is using
//		        dock: 'bottom',
//		        displayInfo: true
//		    };
		    this.tbar = this.tbar.concat(this.gridTbar);
//	    	this.dockedItems = [];
//	    	this.dockedItems.push(pagingtoolbar);
	    	
		    this.callParent(arguments);
		    
		    var btConfig = this.btConfig;
		    if(!btConfig.add){
		    	this.down('button[name=add]').hide();
		    }
		    if(!btConfig.edit){
		    	this.down('button[name=edit]').hide();
		    }
		    if(!btConfig.del){
		    	this.down('button[name=del]').hide();
		    }
		    if(!btConfig.refresh){
		    	this.down('button[name=refresh]').hide();
		    }
		    
	    }
	});
})()
